package cn.itsource.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.itsource.constants.ErrorCode;
import cn.itsource.constants.RConst;
import cn.itsource.mapper.DriverSettingMapper;
import cn.itsource.mapper.DriverSummaryMapper;
import cn.itsource.pojo.domain.DriverAuthMaterial;
import cn.itsource.mapper.DriverAuthMaterialMapper;
import cn.itsource.pojo.domain.DriverSetting;
import cn.itsource.pojo.domain.DriverSummary;
import cn.itsource.pojo.dto.DriverAuditDto;
import cn.itsource.pojo.vo.DriverSummaryVo;
import cn.itsource.result.JSONResult;
import cn.itsource.service.IDriverAuthMaterialService;
import cn.itsource.utils.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 司机实名资料 服务实现类
 * </p>
 *
 * @author lifan
 * @since 2024-09-03
 */
@Service
public class DriverAuthMaterialServiceImpl extends ServiceImpl<DriverAuthMaterialMapper, DriverAuthMaterial> implements IDriverAuthMaterialService {
    @Autowired
    private DriverAuthMaterialMapper driverAuthMaterialMapper;
    @Autowired
    private DriverSummaryMapper driverSummaryMapper;

    @Autowired
    private DriverSettingMapper driverSettingMapper;
    @Override
    public boolean save(DriverAuthMaterial entity) {
        int count = super.count(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getDriverId,entity.getDriverId())
                .ne(DriverAuthMaterial::getRealAuthStatus, RConst.Num.INT_2));
        if (count == 0){
            entity.setId(entity.getDriverId());
            entity.setRealAuthStatus(0);
            entity.setIdcardHolding(entity.getIdcardFront());
            entity.setDrcardHolding(entity.getDrcardFront());
            entity.setDrcardBack(entity.getIdcardBack());
            entity.setCreateTime(new Date());
            return super.save(entity);
        }
        return false;
    }

    /**
     * 审核认证
     * @param driverAuditDto
     * @return
     */
    @Override
    public JSONResult auditAndRevoke(DriverAuditDto driverAuditDto) {
        // 根据审核id查出司机信息
        DriverAuthMaterial driverAuthMaterial = driverAuthMaterialMapper.selectById(driverAuditDto.getId());
        if (driverAuthMaterial != null){
            // 认证审核
            if (driverAuditDto.getRevokeAndAuditStatus() == RConst.Num.INT_1){
                //审核成功就初始化一下总结表
                DriverSummary driverSummary = new DriverSummary();
                driverSummary.setId(driverAuditDto.getId());
                driverSummary.setCreateTime(new Date());
                driverSummaryMapper.insert(driverSummary);
                //初始化司机的配置（就是司机接单距离这些）
                DriverSetting driverSetting = new DriverSetting();
                driverSetting.setId(driverAuditDto.getId());
                driverSetting.setOrderDistance(RConst.Num.INT_50);//订单距离默认值
                driverSetting.setRangeDistance(RConst.Num.INT_5);//接单距离，默认5公里
                driverSetting.setListenService(false);//不自动听单
                driverSettingMapper.insert(driverSetting);
                //审核状态改为1通过审核
                driverAuthMaterial.setRealAuthStatus(RConst.Num.INT_1);
                return  JSONResult.success(driverAuthMaterialMapper.updateById(driverAuthMaterial));
                //撤销审核
            }else {
                //审核状态改为0撤销审核
                //撤销时删除这个司机初始化的总结表
                driverSummaryMapper.deleteById(driverAuditDto.getId());
                driverAuthMaterial.setRealAuthStatus(RConst.Num.INT_0);
                return JSONResult.success(driverAuthMaterialMapper.updateById(driverAuthMaterial));
            }
        }
        return JSONResult.error(ErrorCode.NO_AUDIT_FAILED);
    }

    @Override
    public JSONResult driverState() {
        // 获取司机id
        long driverId = StpUtil.getLoginIdAsLong();
        //查询司机信息
        DriverAuthMaterial one = this.getOne(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getDriverId, driverId)
                .eq(DriverAuthMaterial::getRealAuthStatus, RConst.Num.INT_1));
        DriverSummaryVo driverSummaryVo = new DriverSummaryVo();
        if (one != null){
            // 查询司机的总结表
            DriverSummary driverSummary = driverSummaryMapper.selectById(driverId);
            driverSummaryVo.setRealAuthStatus(true);
            driverSummaryVo.setTodayIncome(driverSummary.getTodayIncome());
            driverSummaryVo.setDriveDuration(driverSummary.getDriveDuration());
            driverSummaryVo.setTodayTradeOrders(driverSummary.getTodayTradeOrders());
            return JSONResult.success(driverSummaryVo);
        }else {
            driverSummaryVo.setRealAuthStatus(false);
            return JSONResult.success(driverSummaryVo);
        }
    }
}
